package com.core.family.biz.yizhao.controller;

import cn.hutool.core.date.DateUtil;
import com.core.common.utils.ValidationUtil;
import com.core.family.biz.yizhao.domain.po.YzOrder;
import com.core.family.biz.yizhao.domain.po.YzOrderPrice;
import com.core.family.biz.yizhao.domain.req.YzOrderReq;
import com.core.family.biz.yizhao.service.YzOrderPriceService;
import com.core.family.biz.yizhao.service.YzOrderService;
import com.core.family.biz.yizhao.service.YzWalletService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.common.annotation.MenuDesc;
import com.core.common.domain.response.R;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;


/**
 * @author honglei
 * @version 1.0
 * @time: 2023-7-1 8:26:43
 * @Description 服务实现层 一招订单表
 */
@MenuDesc(desc = "一招订单表", code = "yzOrder")
@Api(tags = "YzOrderController 一招订单表")
@RestController
@RequestMapping("/api/YzOrder/")
public class YzOrderController {

    @Autowired
    YzOrderService service;


    /**
     * 根据id获取详情
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping(value = "get/{id}")
    public R get(HttpServletRequest request, @PathVariable("id") Long id) {
        return R.ok(service.getById(id));
    }


    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "getPage", method = {RequestMethod.GET, RequestMethod.POST})
    public R getPage(@RequestBody PageQuery<YzOrderReq> pageQuery) {

        PageQuery<BaseQuery> baseQuery = new PageQuery<>();
        baseQuery.setQueryBean(pageQuery.getQueryBean());
        baseQuery.setPageIndex(pageQuery.getPageIndex());
        baseQuery.setPageSize(pageQuery.getPageSize());
        return R.ok(service.selectPage(baseQuery));
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "getDetailPage", method = {RequestMethod.POST})
    public R getDetailPage(@RequestBody PageQuery<YzOrderReq> pageQuery) {

        PageQuery<BaseQuery> baseQuery = new PageQuery<>();
        baseQuery.setQueryBean(pageQuery.getQueryBean());
        baseQuery.setPageIndex(pageQuery.getPageIndex());
        baseQuery.setPageSize(pageQuery.getPageSize());
        return R.ok(service.getDetailPage(baseQuery));
    }

    /**
     * 批量查询
     *
     * @return
     */
    @RequestMapping(value = "getList", method = {RequestMethod.GET, RequestMethod.POST})
    public R getList(@RequestBody YzOrderReq queryReq) {
        return R.ok(service.selectList(queryReq));
    }

    /**
     * 普通更新
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "update")
    public R update(@RequestBody YzOrder bean) {
        return R.ok(service.updateById(bean));
    }


    /**
     * 新增
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "save")
    public R save(@RequestBody YzOrder bean) {
        if (Objects.isNull(bean.getOrderNo())) {
            String orderNo = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
            bean.setOrderNo(orderNo);
        }
   //     ValidationUtil.validateObject(bean);
        return R.ok(service.save(bean));
    }

    /**
     * 删除
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "del/{id}")
    public R del(HttpServletRequest request, @PathVariable("id") Long id) {
        return R.ok(service.removeById(id));
    }

    /**
     * 批量删除
     *
     * @param request
     * @param ids
     * @return
     */
    @PostMapping(value = "batch/del")
    public R batchDel(HttpServletRequest request, @RequestBody List<String> ids) {

        return R.ok(service.removeByIds(ids));
    }


    @PostMapping(value = "submit/{id}")
    public R submit(HttpServletRequest request, @PathVariable("id") Integer id) {
        YzOrder dbOrder = service.getById(id);
        ValidationUtil.validateObject(dbOrder);
        if (null == dbOrder) {
            return R.error("订单不存在");
        }
        if (Objects.isNull(dbOrder.getProjectAddress())) {
            return R.error("信息不完整,缺少项目地址");
        }
        if (!"待发单".equals(dbOrder.getStatus())) {
            return R.error("当前订单状态不允许发单");
        }


        YzOrder bean = new YzOrder();
        bean.setId(id);
        bean.setStatus("发单中");
        service.updateById(bean);
        return R.ok();
    }

    /***
     * 催单
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "reminder/{id}")
    public R reminder(HttpServletRequest request, @PathVariable("id") Integer id) {
        YzOrder dbOrder = service.getById(id);

        return R.ok();
    }




    /***
     * 强制订单完成
     * 1、修改订单状态
     * 2、转移下单人冻结
     * 2、 解冻接单人 冻结收入
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "done/{id}")
    public R done(HttpServletRequest request, @PathVariable("id") Integer id) {


        return R.ok(service.done(id));
    }





    /***
     * 取消订单
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "cancel/{id}")
    public R cancel(HttpServletRequest request, @PathVariable("id") Integer id) {
        YzOrder dbOrder = service.getById(id);
        if (null == dbOrder) {
            return R.error("订单不存在");
        }
        if (!"发单中".equals(dbOrder.getStatus())) {
            return R.error("当前订单状态不允许发单");
        }
        YzOrder bean = new YzOrder();
        bean.setId(id);
        bean.setStatus("订单取消");
        service.updateById(bean);
        // TODO: 2023/7/7  状态变更金额专转移
        return R.ok();
    }


    @Autowired
    YzOrderPriceService priceService;

    @Autowired
    YzWalletService walletService;


    /***
     * 选中竞价
     *  1、订单状态修改 接单人信息冗余补全
     *  2、转移下单人资金到 接单人
     *  3、临时冻结 接单人 收到资金
     * @param bean
     * @return
     */
    @PostMapping(value = "selectPrice")
    public R selectPrice(@RequestBody YzOrder bean) {
        YzOrder dbOrder = service.getById(bean.getId());
        if (null == dbOrder) {
            return R.error("订单不存在");
        }
        if (!"发单中".equals(dbOrder.getStatus())) {
            return R.error("当前订单状态不允许选中竞价");
        }

        dbOrder.setOrderPriceId(bean.getOrderPriceId());

        return R.ok(service.selectPrice(dbOrder));
    }


}
